package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Product;
import ebedynky.beans.ProductSpecification;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * @author Ondrej David (davidon1)
 */
public class ProductSpecificationService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieces the session (connection).
     */
    public ProductSpecificationService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds specification to the db.
     * @param spec ProductSpecification to be inserted
     * @return ID of the added ProductSpecification
     */
    public int addProductSpecification(ProductSpecification spec) {
        session.beginTransaction();
        int id = getMaxProductSpecificationID() + 1;
        spec.setProductSpecificationID(id);
        id = (Integer) session.save(spec);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of ProductSpecifications.
     * @return Max ID
     */
    public int getMaxProductSpecificationID() {
        String hql = "select max(p.productSpecificationID) from ProductSpecification p";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxspecid = (Integer) list.get(0);
            return maxspecid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a ProductSpecification identified by the ID.
     * @param id of the ProductSpecification
     * @return ProductSpecification with the ID given (null if not found)
     */
    public ProductSpecification getProductSpecificationByID(int id) {
        session.beginTransaction();
        ProductSpecification spec = (ProductSpecification) session.get(ProductSpecification.class, new Integer(id));
        session.getTransaction().commit();
        return (spec);
    }

    /**
     * Retrieves a list of all ProductSpecifications stored in the db.
     * @return List of all ProductSpecifications (null if no ProductSpecifications)
     */
    public List<ProductSpecification> getAllProductSpecifications() {
        session.beginTransaction();
        List<ProductSpecification> specList = session.createQuery("from ProductSpecification").list();
        session.getTransaction().commit();
        return (specList);
    }

    /**
     * Updates a specified ProductSpecification identified by ID.
     * @param newSpec an updated ProductSpecification
     */
    public void updateProductSpecification(ProductSpecification newSpec) {
        session.beginTransaction();
        ProductSpecification spec = (ProductSpecification) session.get(Product.class, new Integer(newSpec.getProductSpecificationID()));
        spec.setProductSpecificationID(newSpec.getProductSpecificationID());
        spec.setSpecification(newSpec.getSpecification());

        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the ProductSpecification from db.
     * @param id ProductSpecification ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteProductSpecification(int id) {
        try {
            session.beginTransaction();
            ProductSpecification spec = (ProductSpecification) session.get(ProductSpecification.class, new Integer(id));
            session.delete(spec);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }

    /**
     * Returns a list containing ProductSpecifications ordered by name.
     * @param name of the ProductSpecification to be selected
     * @return List of ProductSpecifications, List containing ProductSpecification with match, null if product not found
     */
    public List<ProductSpecification> getProductSpecificationByName(String name) {
        session.beginTransaction();
        List<ProductSpecification> spec = null;
        if (name.equals("") || name == null) {
            spec = session.createCriteria(ProductSpecification.class).addOrder(Order.asc("specification")).list();
        } else {
            spec = session.createCriteria(ProductSpecification.class).add(Restrictions.like("specification", "%" + name + "%")).addOrder(Order.asc("specification")).list();
        }
        session.getTransaction().commit();
        return (spec);
    }
}
